package org.androidpush.fragment;

import java.util.ArrayList;
import java.util.List;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.IntentFilter;
import android.support.v4.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

import org.androidpush.R;
import org.androidpush.activity.NotificationDetailsActivity;
import org.androidpush.adapter.MessAdapter;
import org.androidpush.client.Constants;
import org.androidpush.client.Notifier;
import org.androidpush.greendao.NotificationMsg;
import org.androidpush.greendao.NotificationMsgDao;
import org.androidpush.utils.ConstantUtils;
import org.androidpush.utils.DBUtils;


public class MessFragment extends Fragment {

    private ListView lvMessage;
    private MessAdapter messAdapter;
    private View view;
    private NotificationMsgDao notificationMsgDao;
    private List<NotificationMsg> notificationMsgList;
    private NotificationReceiver notificationReceiver;

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.xml_mess, container, false);
        init();
        setListViewItemListener();

        IntentFilter filter = new IntentFilter();
        notificationReceiver = new NotificationReceiver();
        filter.addAction(Constants.ACTION_SHOW_NOTIFICATION);
        filter.addAction(Constants.ACTION_NOTIFICATION_CLICKED);
        filter.addAction(Constants.ACTION_NOTIFICATION_CLEARED);
        view.getContext().registerReceiver(notificationReceiver, filter);
        return view;
    }


    private void init() {
        notificationMsgDao = DBUtils.getNotificationMsgDao(this.getActivity());
        lvMessage = (ListView) view.findViewById(R.id.id_mess_lv);
        notificationMsgList = new ArrayList<>();
        notificationMsgList = notificationMsgDao.queryBuilder().list();

        if (notificationMsgList.size() <= 0 || notificationMsgList == null) {
            NotificationMsg msg = new NotificationMsg();
            msg.setMsgTitle(getResources().getString(R.string.str_mess_title_simple));
            msg.setMsgContent(getResources().getString(R.string.str_mess_content_simple));
            msg.setMsgDate(ConstantUtils.getFormatDate());
            msg.setMsgFrom("@Jxht");
            notificationMsgList.add(msg);
            Log.d("MessFragment","notificationMsgList");
        }
        messAdapter = new MessAdapter(this.getActivity(), notificationMsgList);
        lvMessage.setAdapter(messAdapter);
    }


    private void setListViewItemListener() {
        lvMessage.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                NotificationMsg msg = notificationMsgList.get(position);
                Log.d("Msg", msg.toString());
                Intent intent = new Intent();
                intent.setClass(view.getContext(), NotificationDetailsActivity.class);
                intent.putExtra(ConstantUtils.MESS_TITLE, msg.getMsgTitle());
                intent.putExtra(ConstantUtils.MESS_CONTENT, msg.getMsgContent());
                intent.putExtra(ConstantUtils.MESS_DATE, msg.getMsgDate());
                intent.putExtra(ConstantUtils.MESS_FROM, msg.getMsgFrom());
                intent.putExtra("Activity", true);
                startActivity(intent);
                getActivity().overridePendingTransition(R.anim.push_left_in,
                        R.anim.push_left_out);
            }
        });
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        view.getContext().unregisterReceiver(notificationReceiver);
    }

    public final class NotificationReceiver extends BroadcastReceiver {
        final String LOGTAG = "NotificationReceiver";

        @Override
        public void onReceive(Context context, Intent intent) {
            Log.d("NotificationReceiver", "NotificationReceiver.onReceive()...");
            String action = intent.getAction();

            if (Constants.ACTION_SHOW_NOTIFICATION.equals(action)) {
                String notificationId = intent
                        .getStringExtra(Constants.NOTIFICATION_ID);
                String notificationApiKey = intent
                        .getStringExtra(Constants.NOTIFICATION_API_KEY);
                String notificationTitle = intent
                        .getStringExtra(Constants.NOTIFICATION_TITLE);
                String notificationMessage = intent
                        .getStringExtra(Constants.NOTIFICATION_MESSAGE);
                String notificationUri = intent
                        .getStringExtra(Constants.NOTIFICATION_URI);
                String notificationMsgDate = intent.getStringExtra(Constants.NOTIFICATION_DATE);
                String notificationMsgFrom = intent.getStringExtra(Constants.NOTIFICATION_FROM);

                Log.d(LOGTAG, "notificationId=" + notificationId);
                Log.d(LOGTAG, "notificationApiKey=" + notificationApiKey);
                Log.d(LOGTAG, "notificationTitle=" + notificationTitle);
                Log.d(LOGTAG, "notificationMessage=" + notificationMessage);
                Log.d(LOGTAG, "notificationUri=" + notificationUri);
                Log.d(LOGTAG, "notificationDate=" + notificationMsgDate);
                Log.d(LOGTAG, "notificationFrom=" + notificationMsgFrom);

                NotificationMsg notificationMsg = new NotificationMsg();
                notificationMsg.setMsgTitle(notificationTitle);
                notificationMsg.setMsgContent(notificationMessage);
                notificationMsg.setMsgFrom(notificationMsgFrom);
                notificationMsg.setMsgDate(notificationMsgDate);
                notificationMsg.setMsgUri(notificationUri);
                Notifier notifier = new Notifier(getActivity());
                notifier.notify(notificationId, notificationApiKey,
                        notificationTitle, notificationMessage, notificationUri, notificationMsgDate, notificationMsgFrom);

                notificationMsgDao.insertOrReplace(notificationMsg);
                notificationMsgList.add(0,notificationMsg);
                messAdapter.notifyDataSetChanged();
                lvMessage.setSelection(lvMessage.getCount()-1);
            }
        }
    }
}
